﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace PlutoStudio.Web.Mvc.Html
{
    public static partial class SelectListExtensions
    {
        public static MvcHtmlString CheckBoxList<TModel>(this HtmlHelper<TModel> helper, string name, IEnumerable<SelectListItem> items, int index, IDictionary<string, object> htmlAttributes)
        {

            if (items == null || string.IsNullOrWhiteSpace(name))
            {
                return MvcHtmlString.Empty;
            }
            var value = helper.ViewData.Eval(name);
            IEnumerable<string> values;
            if (value != null)
            {

                if (typeof(IEnumerable).IsAssignableFrom(value.GetType()))
                {
                    values = (value as IEnumerable).Cast<object>().Select(v => v.ToString()).ToList();
                }
                else
                {
                    values = value.ToString().Split(',');
                }
            }
            else
            {
                values = new List<string>();
            }
            var sb = new StringBuilder();
            foreach (var item in items)
            {
                var id = string.Format("{0}_{1}", name, index++);
                var tag = new TagBuilder("input");
                tag.MergeAttribute("type", "checkbox");
                tag.MergeAttribute("name", name);
                tag.MergeAttribute("id", id);
                tag.MergeAttribute("value", item.Value);
                if (values.Any(v => v == item.Value))
                {
                    tag.MergeAttribute("checked", "checked", true);
                }
                var label = new TagBuilder("label");
                label.MergeAttribute("for", id);
                label.MergeAttributes(htmlAttributes, true);
                label.InnerHtml = (tag.ToString(TagRenderMode.SelfClosing) + item.Text);
                sb.AppendLine(label.ToString());
            }
            return MvcHtmlString.Create(sb.ToString());
        }

        public static MvcHtmlString CheckBoxList<TModel>(this HtmlHelper<TModel> helper, string name, IEnumerable<SelectListItem> items, int index, object htmlAttributes)
        {
            return CheckBoxList(helper, name, items, index, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }

        public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> items, IDictionary<string, object> htmlAttributes)
        {
            if (items == null || expression == null)
                return MvcHtmlString.Empty;
            var name = ExpressionHelper.GetExpressionText(expression);
            return CheckBoxList(helper, name, items, 0, htmlAttributes);
        }
        public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> items, object htmlAttributes)
        {
            return CheckBoxListFor(helper, expression, items, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));

        }

        public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> items)
        {
            return CheckBoxListFor(helper, expression, items, null);
        }

        public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> items, int index, IDictionary<string, object> htmlAttributes)
        {
            if (items == null || expression == null)
                return MvcHtmlString.Empty;
            var name = ExpressionHelper.GetExpressionText(expression);
            return CheckBoxList(helper, name, items, index, htmlAttributes);
        }
        public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> items, int index, object htmlAttributes)
        {
            return CheckBoxListFor(helper, expression, items, index, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));

        }

        public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> items, int index)
        {
            return CheckBoxListFor(helper, expression, items, index, null);
        }

    }
}
